package com.imooc.sell.utils.wang;

import com.imooc.sell.dao.OrderMasterDao;
import com.imooc.sell.dataobject.OrderMaster;
import com.imooc.sell.enums.ErrorEnum;
import com.imooc.sell.enums.PayStatusEnum;
import com.imooc.sell.expection.SellExpection;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class JudgeUtils {
    /**
     * 判断订单是否存在
     *
     * @param orderId   订单号
     * @param masterDao 用于调用dao层方法
     * @return 成功时的订单对象
     */
    public static OrderMaster OrderIsExist(String orderId, OrderMasterDao masterDao) {
        OrderMaster master;
        try {
            master = masterDao.getOne(orderId);
        } catch (Exception e) {
            log.error("未查找到指定订单,订单号：{}", orderId);
            throw new SellExpection(ErrorEnum.ORDER_NOT_EXIST);
        }

        return master;
    }

    /**
     * 判断订单状态
     *
     * @param srcStatus 被判断状态
     * @param deStatus  目标状态
     */
    public static void judgeOrderStatus(String eMsg, Short srcStatus, Short deStatus) {
        if (!srcStatus.equals(deStatus)) {
            log.error(eMsg + "订单状态错误{}==={}", srcStatus, deStatus);
            throw new SellExpection(ErrorEnum.ORDER_STATUS_ERROR);
        }
    }

    /**
     * 判断订单支付状态
     *
     * @param srcStatus 被判断状态
     * @param deStatus  目标状态
     */
    public static void judgePayStatus(String eMsg, Short srcStatus, Short deStatus) {
        if (!srcStatus.equals(deStatus)) {
            log.error(eMsg + ",订单支付状态错误");
            throw new SellExpection(ErrorEnum.PAY_STATUS_ERROR);
        }
    }
}
